#!/usr/bin/python
# -*- coding: utf-8 -*-

##
 # Copyright 2009 Caio Souza
 # This file is part of Rpyg.
 #
 # Rpyg is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # Rpyg is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with Rpyg. If not, see <http://www.gnu.org/licenses/>.
 ##

'''Módulo das camadas de um frame.'''

import os

import pygame

from rpyg.Blitable import Blitable

class Layer (Blitable):
    surface_reference = {}
    '''Dicionário de referência das imagens carregas.'''
    
    def __init__ (self, filename, rect):
        '''Cria uma camada a partir do arquivo e do retângulo passados.
        
        Carrega o arquivo passado por 'filename' como uma superfície cria
        uma camada usando somente o ret�ngulo especificado.
        '''
        
        Blitable.__init__ (self)
        
        if isinstance(filename, tuple): filename = os.path.join (*filename)
        self._surface = self._loadSurface (filename)
        self._surface = self._surface.subsurface (rect)
        
    def _loadSurface (self, filename):
        '''Carrega a imagem a partir do arquivo ou do dicionário de referência.'''
        
        file = self.surface_reference.get (filename)
        if file is not None: return file
        file = pygame.image.load (filename).convert_alpha ()
        self.surface_reference[filename] = file
        return file
    